<?php 
/**
 * Yee
 * a litehtweight framework
 *
 * EXAMPLE:	
 * Yee::set();
 * Yee::display();
 * Yee::call();
 * Yee::ajaxReturn();
 *
 * @author zhfu<mrhanta.163.com>
 * @vertion 1.1
 */

//Yee Setting;
if(!defined('YEE_URL_REWRITE')) 
	define('YEE_URL_REWRITE',0);
	
if(!defined('APP_ROOT')){
	if(YEE_URL_REWRITE==1)
		define('APP_ROOT',dirname($_SERVER['SCRIPT_NAME']));
	else
		define('APP_ROOT',$_SERVER['SCRIPT_NAME']);
}

class Yee{
	//模板引擎;
	public static $TplEngine;
	public static $Actions;
	public static $ActName;
	/**
	 * @method 
	 */
	public static function run($act=array()){
		Yee::setActions($act);
		Yee::setup();
		Yee::clearInput();
		Yee::urlapache();
	}
	
	public function setActions($act){
		foreach($act as $key=>$value){
			Yee::$Actions[$key]=$value;
		}
	}
	
	public static function clearInput(){
		//过滤输入字符;
		foreach($_GET as $key=>$value)
			$_GET[$key]=trim(strip_tags(urldecode($value)));
		foreach($_POST as $key=>$value)
			$_GET[$key]=trim(strip_tags(urldecode($value)));
	}
	
	public static function clearArgv($argv){
		$argv=trim(strip_tags(urldecode($argv)));
		if(empty($argv) || $argv==false)
			return false;
		else
			return $argv;
	}
	
	//url导航;
	public static function urlapache(){
		$act=Yee::getAction();
		$func=array_shift($act);
		if(isset(Yee::$Actions[$func])){
			call_user_func_array(Yee::$Actions[$func],$act);
		}else{
			call_user_func('init');
		}
	}
	
	public static function getAction(){
		//解析url;
		$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
		$url=str_replace(APP_ROOT.'/','',$path,$matches);
		return array_filter(explode('/',$url),"Yee::clearArgv");
	}
	
	public static function setup(){
		Yee::import('rain.tpl');
		Yee::$TplEngine=new RainTPL;
		Yee::$TplEngine->assign('Yee',array(
			'APP_ROOT'=>APP_ROOT
		));	
	}

	public static function import($modules){
		foreach(explode(',',$modules) as $mod){
			require dirname(__FILE__).'/Yee/'.$mod.'.class.php';
		}
	}
	
	public static function set($argv,$value){
		Yee::$TplEngine->assign($argv,$value);
	}
	
	public static function display($tpl){
		Yee::$TplEngine->draw($tpl);
	}
	
	public static function redirect($func){
		list($module,$method)=explode(':',$func);
		if($module==''){
			header("Location: ".APP_ROOT."/$method");
		}else{
			//判断url模式;
			if(1!=YEE_URL_REWRITE)
				$module=$module.'.php';
				
			header("Location: ".(1==YEE_URL_REWRITE?APP_ROOT:dirname(APP_ROOT))."/$module/$method");
			
		}
	}
}

?>